*****************************************************
* OVERVIEW
*	FILE: 0-run-all.do
*   This script runs the code to generate the tables and figures for the paper:
*		"Economic Geography and Air Pollution Regulation in the United States" 
*	AUTHORS: Alex Hollingsworth, Taylor Jaworski, Carl Kitchens, and Ivan Rudik
* 	VERSION: April 2024

* DESCRIPTION
* 	This script replicates the analysis in our paper and online appendix
*   All raw data are stored in raw/
*   All processed data are stored in data/
*   All code is stored in scripts/
*   All tables and figures are outputted to output/
* 
* SOFTWARE REQUIREMENTS
*   Analyses run on Mac OSX 14.4.1 using 
*	- Stata version 18.1, 
*	- Matlab version 24.1.0.2537033 (R2024a),  
*	- R version 4.3.1, 
*	- and Julia version 1.9
*
* TO PERFORM A CLEAN RUN, 
* 	1. Delete the following three directories:
*   	data/
*   	output/
*		temp/
*   2. Ensure that lines 79 - 83 are correct for your machine in `0-run-all.do`
*	2. Open the stata project `economic-geography-air-pollution-regulation.stpr` or make the working directory of Stata is the same directory `economic-geography-air-pollution-regulation.stpr`
*		 is located in
* 	3. Run this file, `0-run-all.do`

*****************************************************
// Clear stored objects and set preferences
clear all
matrix drop _all // Drop everything in mata
set more off
set varabbrev off


*****************************************************
// Local switches

* Switch log on/off
local log 1

* Switches for running individual do files
local unzip_files			1
local run_build				1
local run_analysis 			1
local run_model				1
local run_model_analysis	1
local run_in_text			1

* Initialize log and record system parameters
local log 1

cap mkdir "logs"
cap log close
set linesize 255 // Specify screen width for log files
local datetime : di %tcCCYY.NN.DD!_HH.MM.SS `=clock("$S_DATE $S_TIME", "DMYhms")'
local logfile "logs/log_`datetime'.txt"
if `log' {
	log using "`logfile'", text
}

di "Begin date and time: $S_DATE $S_TIME"
di "Stata version: `c(stata_version)'"
di "Updated as of: `c(born_date)'"
di "Variant:       `=cond( c(MP),"MP",cond(c(SE),"SE",c(flavor)) )'"
di "Processors:    `c(processors)'"
di "OS:            `c(os)' `c(osdtl)'"
di "Machine type:  `c(machine_type)'"
*****************************************************

// Set root directory

// Set shell command paths. 
	global R_PATH "/usr/local/bin/R"
	global RSCRIPT_PATH "/usr/local/bin/Rscript"
	global MATLAB_PATH "/Applications/MATLAB_R2024a.app/bin/matlab"
	global JULIA_PATH "/Applications/Julia-1.9.app/Contents/Resources/julia/bin/julia"
	global num_cores 8
	
if `unzip_files' {
 
	shell unzip "raw_1.zip" 
	shell rm -r "__MACOSX"
	
	shell unzip "raw_2.zip" 
	shell rm -r "__MACOSX"
	
	mkdir raw
	shell cp -R raw_1/ raw
	shell cp -R raw_2/ raw
	shell rm -r raw_1
	shell rm -r raw_2

// Unzip R and stata libraries 
	shell unzip "scripts/0-libraries.zip" -d scripts/
	shell rm -r "scripts/__MACOSX"
	shell rm -r "__MACOSX"
	
// Install Julia packages
shell $JULIA_PATH --project=. "scripts/5-model/startup.jl"

	// Unzip renv (libraries for R)
	shell unzip "renv.zip"
	shell rm -r "__MACOSX"
}
*****************************************************
// Create project directories 
*****************************************************

cap mkdir "temp"
cap mkdir "output"
cap mkdir "data"
cap mkdir "data/modified-ap3"
cap mkdir "data/tradecosts"
cap mkdir "data/tradeflows"
cap mkdir "data/BEA"
cap mkdir "data/greenbook"
cap mkdir "data/IRS"
cap mkdir "data/counterfactual"
cap mkdir "data/counterfactual/inputs"

*****************************************************
* Make sure libraries are set up correctly
*****************************************************

	* Disable locally installed Stata programs
	cap adopath - PERSONAL
	cap adopath - PLUS
	cap adopath - SITE
	cap adopath - OLDPLACE
	cap adopath - "scripts/0-libraries/stata"
	
	* Create and define a local installation directory for the packages
	net set ado "scripts/0-libraries/stata"
	
	adopath ++ "scripts/0-libraries/stata"

// Build dataset
if `run_build' == 1 {
	
	/* Run modified AP3 code to extract how one ton of pollution released in 
	any given county will affect mortality for a single prime aged worker in 
	every county in US, in VSL terms */
	shell $R_PATH --vanilla <"scripts/1-modified-ap3/1-0-set-up-for-modified-ap3.R"	
	shell "$MATLAB_PATH" -nosplash -nodesktop -r "run('scripts/1-modified-ap3/PM_CRDM_Marginal.m');exit;"

	// Find Tau
	do "scripts/2-find-tau/1-0-data-tradecosts.do"
	do "scripts/2-find-tau/2-0-data-averagevalue.do"
	shell "$MATLAB_PATH" -nosplash -nodesktop -r "run('scripts/2-find-tau/find_tau.m');exit;"


	// Build analysis datasets
	shell $R_PATH --vanilla <"scripts/3-build-dataset/1-0-data-nonattainment.R"	
	shell $R_PATH --vanilla <"scripts/3-build-dataset/2-0-data-pay-cf.R"	
	do "scripts/3-build-dataset/3-0-data-county-bea.do"	
	shell $R_PATH --vanilla <"scripts/3-build-dataset/4-0-data-productivity.R"	
	shell $R_PATH --vanilla <"scripts/3-build-dataset/5-0-data-emissions.R"	
	shell $R_PATH --vanilla <"scripts/3-build-dataset/6-0-data-fix-virginia-fips-cf.R"	
	shell $R_PATH --vanilla <"scripts/3-build-dataset/7-0-data-migration-estimation.R"	
	shell $R_PATH --vanilla <"scripts/3-build-dataset/8-0-data-amenities.R"	
	shell $R_PATH --vanilla <"scripts/3-build-dataset/9-0-data-migration-cf.R"	
	shell $R_PATH --vanilla <"scripts/3-build-dataset/10-0-naaqs-standards.R"	
	shell $R_PATH --vanilla <"scripts/3-build-dataset/11-0-1997-nei.R"	
	shell $R_PATH --vanilla <"scripts/3-build-dataset/12-0-trim-ap3-for-model.R"	
	
}

// Run empirical analyses
if `run_analysis' == 1 {
	
	shell $R_PATH --vanilla <"scripts/4-empirical-results/2-0-empirical-results.R"	
	
}

// Run Model
if `run_model' == 1 {
	
	shell $JULIA_PATH -p $num_cores --project= "scripts/5-model/1-0-cf-main.jl"
	
}

// Run analysis of model results
if `run_model_analysis' == 1 {

	shell $R_PATH --vanilla <"scripts/5-model/4-0-model-results.R"	
	shell $R_PATH --vanilla <"scripts/5-model/5-0-summary-plots.R"	
	shell $R_PATH --vanilla <"scripts/5-model/6-0-model-tables.R"	
	cap rm "Rplots.pdf"

}

// In-text numbers	
if `run_in_text' == 1 {

	shell $R_PATH --vanilla <"scripts/6-in-text-numbers/1-0-in-text-numbers.R"		
	
}


	
log close 
	
